/**
 * 样式工具类
 * 提供常用样式的快捷创建方法
 */

import { Style, Circle, Fill, Stroke, Text, Icon } from 'ol/style';
import { Feature } from 'ol';

/**
 * 样式工具类
 */
export class StyleUtil {
  /**
   * 创建圆形点样式
   * @param options 样式配置
   * @returns Style对象
   */
  static createCircleStyle(options: {
    radius?: number;
    fillColor?: string;
    strokeColor?: string;
    strokeWidth?: number;
    opacity?: number;
  } = {}): Style {
    const {
      radius = 5,
      fillColor = '#3399CC',
      strokeColor = '#fff',
      strokeWidth = 2,
      opacity = 1
    } = options;

    return new Style({
      image: new Circle({
        radius,
        fill: new Fill({
          color: fillColor
        }),
        stroke: new Stroke({
          color: strokeColor,
          width: strokeWidth
        })
      }),
      zIndex: Infinity
    });
  }

  /**
   * 创建图标样式
   * @param options 样式配置
   * @returns Style对象
   */
  static createIconStyle(options: {
    src: string;
    scale?: number;
    anchor?: [number, number];
    anchorXUnits?: 'fraction' | 'pixels';
    anchorYUnits?: 'fraction' | 'pixels';
    opacity?: number;
    rotation?: number;
  }): Style {
    const {
      src,
      scale = 1,
      anchor = [0.5, 1],
      anchorXUnits = 'fraction',
      anchorYUnits = 'fraction',
      opacity = 1,
      rotation = 0
    } = options;

    return new Style({
      image: new Icon({
        src,
        scale,
        anchor,
        anchorXUnits,
        anchorYUnits,
        opacity,
        rotation
      }),
      zIndex: Infinity
    });
  }

  /**
   * 创建文本样式
   * @param options 样式配置
   * @returns Style对象
   */
  static createTextStyle(options: {
    text: string;
    font?: string;
    fillColor?: string;
    strokeColor?: string;
    strokeWidth?: number;
    offsetX?: number;
    offsetY?: number;
    scale?: number;
  }): Style {
    const {
      text,
      font = '12px sans-serif',
      fillColor = '#000',
      strokeColor = '#fff',
      strokeWidth = 2,
      offsetX = 0,
      offsetY = 0,
      scale = 1
    } = options;

    return new Style({
      text: new Text({
        text,
        font,
        fill: new Fill({
          color: fillColor
        }),
        stroke: new Stroke({
          color: strokeColor,
          width: strokeWidth
        }),
        offsetX,
        offsetY,
        scale
      }),
      zIndex: Infinity
    });
  }

  /**
   * 创建线样式
   * @param options 样式配置
   * @returns Style对象
   */
  static createLineStyle(options: {
    color?: string;
    width?: number;
    lineDash?: number[];
    lineDashOffset?: number;
    opacity?: number;
  } = {}): Style {
    const {
      color = '#3399CC',
      width = 2,
      lineDash,
      lineDashOffset = 0,
      opacity = 1
    } = options;

    return new Style({
      stroke: new Stroke({
        color,
        width,
        lineDash,
        lineDashOffset
      }),
      zIndex: Infinity
    });
  }

  /**
   * 创建面样式
   * @param options 样式配置
   * @returns Style对象
   */
  static createPolygonStyle(options: {
    fillColor?: string;
    strokeColor?: string;
    strokeWidth?: number;
    fillOpacity?: number;
    strokeOpacity?: number;
    lineDash?: number[];
  } = {}): Style {
    const {
      fillColor = 'rgba(51, 153, 204, 0.3)',
      strokeColor = '#3399CC',
      strokeWidth = 2,
      fillOpacity = 0.3,
      strokeOpacity = 1,
      lineDash
    } = options;

    // 处理颜色透明度
    const fillColorWithOpacity = this.addOpacityToColor(fillColor, fillOpacity);
    const strokeColorWithOpacity = this.addOpacityToColor(strokeColor, strokeOpacity);

    return new Style({
      fill: new Fill({
        color: fillColorWithOpacity
      }),
      stroke: new Stroke({
        color: strokeColorWithOpacity,
        width: strokeWidth,
        lineDash
      }),
      zIndex: Infinity
    });
  }

  /**
   * 创建聚合点样式（带数量）
   * @param count 聚合数量
   * @param options 样式配置
   * @returns Style对象
   */
  static createClusterStyle(
    count: number,
    options: {
      radius?: number;
      fillColor?: string;
      strokeColor?: string;
      textColor?: string;
      fontSize?: string;
    } = {}
  ): Style {
    const {
      radius = 10,
      fillColor = '#3399CC',
      strokeColor = '#fff',
      textColor = '#fff',
      fontSize = '12px'
    } = options;

    // 根据数量调整半径
    const size = Math.min(radius + Math.log(count) * 2, radius * 3);

    return new Style({
      image: new Circle({
        radius: size,
        fill: new Fill({
          color: fillColor
        }),
        stroke: new Stroke({
          color: strokeColor,
          width: 2
        })
      }),
      text: new Text({
        text: count > 99 ? '99+' : count.toString(),
        fill: new Fill({
          color: textColor
        }),
        font: `bold ${fontSize} sans-serif`
      }),
      zIndex: Infinity
    });
  }

  /**
   * 为颜色添加透明度
   * @param color 颜色值（hex或rgb）
   * @param opacity 透明度 0-1
   * @returns rgba颜色值
   */
  private static addOpacityToColor(color: string, opacity: number): string {
    // 如果已经是rgba格式，提取rgb部分
    if (color.startsWith('rgba')) {
      const match = color.match(/rgba?\(([^)]+)\)/);
      if (match) {
        const rgb = match[1].split(',').slice(0, 3).join(',');
        return `rgba(${rgb}, ${opacity})`;
      }
    }

    // 如果是hex格式，转换为rgb
    if (color.startsWith('#')) {
      const hex = color.slice(1);
      const r = parseInt(hex.slice(0, 2), 16);
      const g = parseInt(hex.slice(2, 4), 16);
      const b = parseInt(hex.slice(4, 6), 16);
      return `rgba(${r}, ${g}, ${b}, ${opacity})`;
    }

    // 如果是rgb格式
    if (color.startsWith('rgb')) {
      const match = color.match(/rgb\(([^)]+)\)/);
      if (match) {
        return `rgba(${match[1]}, ${opacity})`;
      }
    }

    // 默认返回原色
    return color;
  }

  /**
   * 创建组合样式（多个样式叠加）
   * @param styles 样式数组
   * @returns Style数组
   */
  static createCombinedStyles(...styles: Style[]): Style[] {
    return styles;
  }
}

